package com.hlj.ncp.security.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.hlj.ncp.common.core.domain.AjaxResult;
import com.hlj.ncp.security.entity.Inspection;
import com.hlj.ncp.security.service.IInspectionService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 安全巡检信息操作处理
 */
@Controller
@RequestMapping("/security/inspection")
public class InspectionController {
    @Autowired
    private IInspectionService inspectionService;
    
    /**
     * 安全巡检管理页面
     */
    @GetMapping("/index")
    public String index() {
        return "security/inspection/index";
    }

    /**
     * 获取安全巡检列表
     */
    @PreAuthorize("hasAuthority('security:inspection:list')")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(Inspection inspection) {
        List<Inspection> list = inspectionService.selectInspectionList(inspection);
        return AjaxResult.success(list);
    }

    /**
     * 获取安全巡检详细信息
     */
    @PreAuthorize("hasAuthority('security:inspection:query')")
    @GetMapping(value = "/{inspectionId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("inspectionId") Long inspectionId) {
        return AjaxResult.success(inspectionService.selectInspectionById(inspectionId));
    }

    /**
     * 新增安全巡检 - 处理所有POST请求
     */
    @PostMapping
    @ResponseBody
    public AjaxResult add(Inspection inspection) {
        // 处理空请求
        if (inspection == null) {
            inspection = new Inspection();
        }
        
        // 生成巡检编号
        if (inspection.getInspectionCode() == null || inspection.getInspectionCode().isEmpty()) {
            String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            String randomNum = String.format("%04d", new Random().nextInt(10000));
            inspection.setInspectionCode("INS" + dateStr + randomNum);
        }
        
        // 设置默认值
        if (inspection.getInspectionArea() == null || inspection.getInspectionArea().isEmpty()) {
            inspection.setInspectionArea("仓库区域");
        }
        
        if (inspection.getInspector() == null || inspection.getInspector().isEmpty()) {
            inspection.setInspector("安全管理员");
        }
        
        if (inspection.getInspectionType() == null || inspection.getInspectionType().isEmpty()) {
            inspection.setInspectionType("1"); // 默认日常巡检
        }
        
        if (inspection.getInspectionTime() == null) {
            inspection.setInspectionTime(LocalDateTime.now());
        }
        
        if (inspection.getInspectionResult() == null || inspection.getInspectionResult().isEmpty()) {
            inspection.setInspectionResult("0"); // 默认正常
        }
        
        if (inspection.getStatus() == null || inspection.getStatus().isEmpty()) {
            inspection.setStatus("0"); // 默认待处理
        }
        
        return AjaxResult.success(inspectionService.insertInspection(inspection));
    }
    
    /**
     * 处理JSON请求
     */
    @PostMapping(consumes = "application/json")
    @ResponseBody
    public AjaxResult addJson(@RequestBody Inspection inspection) {
        return add(inspection); // 复用上面的方法
    }

    /**
     * 修改安全巡检
     */
    @PreAuthorize("hasAuthority('security:inspection:edit')")
    @PutMapping
    @ResponseBody
    public AjaxResult edit(@RequestBody Inspection inspection) {
        return AjaxResult.success(inspectionService.updateInspection(inspection));
    }

    /**
     * 删除安全巡检
     */
    @PreAuthorize("hasAuthority('security:inspection:remove')")
    @DeleteMapping("/{inspectionIds}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long[] inspectionIds) {
        return AjaxResult.success(inspectionService.deleteInspectionByIds(inspectionIds));
    }
    
    /**
     * 获取安全检查统计数据
     */
    @GetMapping("/stats")
    @ResponseBody
    public AjaxResult getStats() {
        Map<String, Object> stats = inspectionService.getInspectionStats();
        return AjaxResult.success(stats);
    }
} 